package base.java8.time;

import java.time.*;
import java.util.Date;

/**
 * * {@link LocalDateTime} 本地日期和时间
 * * {@link LocalDate}     本地日期和时间
 * * {@link LocalDateTime} 本地日期和时间
 * * {@link ZonedDateTime} 带时区的日期和时间
 * * {@link Instant} 时间轴上单一瞬间点建模,可以用来记录时间戳,可作为转换类与其他时间类进行转换
 * * {@link ZoneId} 时区
 * * {@link ZoneOffset} 偏移量
 * * {@link Duration} 表示秒/纳秒的时间间隔,适合处理较短的时间
 * * {@link Period} 示一段时间的年月日,适合处理较长的时间
 *
 * @author 王霄
 * @date 2021/9/11 22:14
 */
public class TimeDemo {
    public static void main(String[] args) {

        //#############################获取当前时间###############################

        System.out.println("Instant.now() = " + Instant.now());
        System.out.println("LocalDateTime.now() = " + LocalDateTime.now());
        System.out.println("LocalDateTime.now(Clock.systemDefaultZone()) = "
                + LocalDateTime.now(Clock.systemDefaultZone()));
        System.out.println("LocalDateTime.now(Clock.systemUTC()) = "
                + LocalDateTime.now(Clock.systemUTC()));
        System.out.println("LocalDateTime.now(ZoneId.systemDefault()) = "
                + LocalDateTime.now(ZoneId.systemDefault()));
        System.out.println("LocalDateTime.now(ZoneId.of('+0') = "
                + LocalDateTime.now(ZoneId.of("+0")));
        System.out.println("LocalDateTime.now(ZoneId.of('+8')) = "
                + LocalDateTime.now(ZoneId.of("+8")));
        System.out.println("LocalDateTime.now(ZoneOffset.ofHours(0)) = "
                + LocalDateTime.now(ZoneOffset.ofHours(0)));
        System.out.println("LocalDateTime.now(ZoneOffset.ofHours(8)) = "
                + LocalDateTime.now(ZoneOffset.ofHours(8)));
        System.out.println("LocalTime.now() = " + LocalTime.now());
        System.out.println("LocalDate.now() = " + LocalDate.now());
        System.out.println("Year.now() = " + Year.now());
        System.out.println("MonthDay.now() = " + MonthDay.now());
        System.out.println("MonthDay.now() = " + MonthDay.now());


        //#############################获取时间戳###############################
        System.out.println("Instant.now().toEpochMilli() = " + Instant.now().toEpochMilli());
        System.out.println("System.currentTimeMillis() = " + System.currentTimeMillis());
        System.out.println("new Date().getTime() = " + new Date().getTime());


        //#############################Instant转换###############################
        //Instant本身只是一个时刻，不带时区概念，所以转换时需要带上时区
        Instant instant = LocalDateTime.now().toInstant(ZoneOffset.ofHours(0));
        System.out.println("instant = " + instant);
        LocalDateTime localDateTime = instant.atOffset(ZoneOffset.ofHours(0)).toLocalDateTime();
        System.out.println("localDateTime = " + localDateTime);

        System.out.println("Date.from(Instant.now()) = " + Date.from(Instant.now()));
        System.out.println("new Date().toInstant() = " + new Date().toInstant());
        System.out.println("new Date().toInstant().atZone(ZoneId.systemDefault()) = "
                + new Date().toInstant().atZone(ZoneId.systemDefault()));
        System.out.println("new Date().toInstant().atOffset(ZoneOffset.ofHours(8)) = "
                + new Date().toInstant().atOffset(ZoneOffset.ofHours(8)));


        //#############################时间偏移###############################
        //获取1年2个月23天后的日期
        System.out.println(LocalDate.now().plus(Period.of(1, 2, 23)));
        //adjustInto把指定的值赋值给传入的对象，使其拥有相同的时间
        System.out.println(LocalTime.parse("23:59:59").adjustInto(LocalDateTime.now()));

    }
}
